<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Project extends Controller_Page_Template
{
	private $project_id = NULL;
	private $user_id = NULL;
	private $active_tab = NULL;
	
	public function before()
	{
		parent::before();
		$this->project_id = $this->request->param('project_id');
		$this->user_id = Helper_Auth::instance()->get_user_id();
		
		$this->control_navigation = View::factory('/header/control_navigation');
	}
	public function action_test()
	{
		$this->single_cotent = View::factory('/test');
	}
	public function action_index()
	{
		$projects = array();
		
		if (isset($this->project_id))
			$project = Model::factory('project')->get_project_info($this->project_id);
		else
		{
			$projects = Model::factory('project')->get_all_project($this->user_id);
			
			foreach ($projects as $id=>$p)
			{
				$p['progress'] = 50;
				$projects[$id] = $p;
			}	
		}
		//$this->top_header = View::factory('project/partials/top_header',array('project' => isset($project) ? $project : NULL));
		//$this->primary_content = View::factory('/project/index_table', array($this->primary_content = View::factory('/project/index_table', array(
		$this->single_cotent = View::factory('/project/index_table', array(
			'projects' => isset($projects) ? $projects : $project,
			'cur_project' => isset($project) ? $project : NULL 
		));
	}
	public function action_hot_tips()
	{
		$this->primary_content = View::factory('/project/hot_tips');
	}
	public function action_add_project()
	{
		$user_id 	= Helper_Auth::instance()->get_user_id();
		$m 			= Model::factory('project');
		
		if ($_POST)
		{
			$info = $_POST;
			unset($info['save']);
			unset($info['type_name']);
			
			//add new category
			if ($_POST['type'] == '')
			{	
				$ret_add_category 		= $m->add_category($user_id, $_POST['type_name']);
				$info['category_id'] 	= $ret[0];
			}
			$ret = $m->add_project($user_id, $info);
			
			if(!$ret)
			{
				$this->primary_content = View::factory('/project/add_project',array('error' => 'asdas'));
			}
			else 
				$this->request->redirect('/project/'.$ret[0]);
		}
		else 
		{
			//$this->add_sidebar(Helper_MyConst::USER_PRIVILEDGE(NULL,1));
			//$this->top_header = View::factory('project/partials/top_header');
			$this->primary_content = View::factory('/project/add_project');
		}
	}
	public function submit_add_project()
	{
		$data = $_POST;
		unset($data['ok']);
		unset($data['back']);
		$ret = Model::factory('project')->add_project(1, $data);
		$this->request->redirect('/project');
		//echo debug::dump($ret);
	}
	
	public function action_setting()
	{
		$project_id = $this->request->param('project_id');
		$project = Model::factory('project')->get_project_info($project_id);
		//$this->add_sidebar(Helper_MyConst::USER_PRIVILEDGE($project_id,Helper_Auth::instance()->get_user_id()));
		$this->single_cotent = View::factory('/project/setting',
			array('project' => $project
		));
		//$this->top_header = View::factory('project/partials/top_header',array('project' => $project));
	}
	public function action_submit_setting()
	{
		$project_id = $this->request->param('project_id');
		$user_id  	= Helper_Auth::instance()->get_user_id();
		$m 			= Model::factory('project');
		$info 		= $_POST; 
		unset($info['save']);
		unset($info['cancel']);
		
		//Edit new category
		if ($info['type_name'] != $info['category'])
		{
			
			$category = $m->get_category_by_name($info['type_name']);
			if (!$category)
			{
				echo (1);
				$ret = $m->add_category($user_id, $info['type_name']);
				if ($ret)
					$info['category_id'] = $ret[0];
			}
		}
		unset($info['type_name']);
		unset($info['category']);
		
		//updatte project information
		$ret = Model::factory('project')->edit_project_info($project_id, $info);
		if ($ret)
			$this->request->redirect('/project');
		else 
			$this->request->redirect('/project/'.$project_id.'/setting');
	}
	
	public function action_description()
	{
		$project = Model::factory('project')->get_project_info($this->project_id);
		$this->primary_content = View::factory('project/description',
			array('project' => $project
		));
		$this->top_header = View::factory('project/partials/top_header',array('project' => $project));
	}
	
	public function action_add_task()
	{
		if($_POST)
			$this->submit_add_task();
		else 
		{
			$users = Model::factory('user')->get_project_users($this->project_id, 1);
			$this->primary_content = View::factory('task/add_task',array(
				'users' 		=> $users,
				'project_id'	=> $this->project_id
			));
		}
	}
	public function submit_add_task()
	{
		//echo debug::dump($_POST);die();
		
		$t = Model::factory('task');
		$info['user_id'] 		= $this->user_id;
		$info['description']	= '';
		$info['name']			= $_POST['name'];
		$info['attachment']		= '';
		$info['priority']		= $_POST['priority'];
		$info['project_id']		= $this->project_id;
		$info['deadline']		= $_POST['deadline'];
		$info['status']			= Helper_MyConst::TASK_ON_PROGRESS;
		
		$ret = $t->add_task($this->user_id, $info);
		$ret = $t->assign_task($ret[0], $_POST['assign_to']);
		if ($ret)
		{
			$users = Model::factory('user')->get_project_users($this->project_id, 1);
			$this->primary_content = View::factory('/task/add_task', array(
				'success' 		=> 1, 
				'users' 		=> $users,
				'project_id'	=> $this->project_id
			));
		}
		else 
			Request::current()->redirect('/project/'.$this->project_id.'/add_task');
			
	}
	
	public function action_people()
	{
		$this->active_tab = Helper_MyConst::TAB_PEOPPLE;
		
		$users = Model::factory('user')->get_project_users($this->project_id, 1);
		$project = Model::factory('project')->get_project_info($this->project_id);
		
		//$this->top_header = View::factory('project/partials/top_header',array('project' => $project));
		//$this->top_header = View::factory('header/user_optimize');
		$this->single_cotent = View::factory('project/people', array(
			'project'	=> $project,
			'users'		=> $users
		));
	}
	public function action_add_user()
	{
		$this->primary_content = View::factory('/project/add_user', array(
			'project_id'	=> $this->project_id,
		));
	}
	public function action_submit_add_user()
	{
		$user_id = $_POST['user_id'];
		$role 	 = $_POST['role'];
		//echo Debug::dump($_POST);die();
		$ret = Model::factory('project')->add_user_2_project($role, $user_id, $this->project_id);
		if ($ret)
			$this->request->redirect('project/'.$this->project_id.'/people');
		else 
			$this->request->redirect('project/'.$this->project_id.'/add_user');
	}
	public function action_tasks()
	{
		$project = Model::factory('project')->get_project_info($this->project_id);
		//$this->top_header = View::factory('project/partials/top_header',array('project' => $project));
		
		$users = Model::factory('user')->get_project_users($this->project_id, Helper_MyConst::IN_PROJECT);
		$tasks = Model::factory('task')->get_project_tasks($this->project_id);
		$lists = Model::factory('task')->get_project_group($this->project_id);
		//echo Debug::dump($tasks);
		//$this->primary_content = View::factory('/task/index', array(
		$this->single_cotent = View::factory('/task/index', array(
			'users' 	=> $users,
			'tasks' 	=> $tasks,
			'project'	=> $project,
			'lists'		=> $lists
		));
	}
	public function action_milestones()
	{
		$this->active_tab = Helper_MyConst::TAB_MILESTONES;
		
		$project = Model::factory('project')->get_project_info($this->project_id);
		//$this->top_header = View::factory('project/partials/top_header',array('project' => $project));
		$milestones = Model::factory('milestone')->get_project_milestones($this->project_id);
		$users = Model::factory('user')->get_project_users($this->project_id, Helper_MyConst::IN_PROJECT);
		$this->single_cotent = View::factory('/milestones/milestones', array(
			'users' 		=> $users,
			'project'		=> $project,
			'milestones'	=> $milestones
		));
	}
	public function after()
	{
		//$functions = Helper_MyConst::USER_PRIVILEDGE($this->project_id,1);
		//$this->add_sidebar_view(View::factory('/sidebar/project_sidebar', array('functions' => $functions, 'active_tab' => $this->active_tab)));
		
		parent::after();
	}
}

?>